<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Init Model
 *
 */


class Member_model extends CI_Model {

	function __construct(){
		parent::__construct();
	}


	
	//-- CHECK MEMBER LOGGED-IN --//
	function check_logedin() {
		if (!$this->session->userdata('mem_email')) {
			redirect('member/login');
		}
	}
	function getMember($user){
		$sql = "select * from member where id='$user' and status=51 limit 1";
		$query = $this->db->query($sql);
		if ($query->row()){
			return $query->row();
		}
		else{
			return false;
		}
	}
	
	
	
	//-- CHECK MEMBER LOGIN POST --//
	function check_login_submit($post_data) {
		if ($post_data) {
	
			$email = trim($post_data['email']);
			$password = trim($post_data['password']);
	
			$sql = "Select * from member where email='$email' and password='$password' and status=51";
			$query = $this->db->query($sql);
			$user = $query->row();
	
			if ($query->num_rows() == 1) {
				$this->clear_session_data("member");
	
				$this->session->set_userdata('mem_id', $user->id);
				$this->session->set_userdata('mem_email', $user->email);
				$this->session->set_userdata('mem_password', $user->password);
				//$this->session->set_userdata('mem_phone', $user->phone);
				$this->session->set_userdata('mem_type', $user->type);
				$this->session->set_userdata('mem_name', $user->name);
				$this->session->set_userdata('mem_last_login_date', $user->last_login_date);
				$this->session->set_userdata('mem_last_login_ip', $user->last_login_ip);
				$this->session->set_userdata('mem_created_on', $user->created_on);
				//-- Update last login of successfull Login
				$sql = "update member set last_login_date = NOW() where id=$user->id";
				$query = $this->db->query($sql);
				return TRUE;
			} else {
				return FALSE;
			}
		}
	}
	
	function get_member($mem_id){
		if($mem_id){
			$sql="select * from member where id=$mem_id and status=51 ";
			$query = $this->db->query($sql);
			if ($query->row())
				return $query->row();
			else
				return false;
		}else{
			return false;
		}
	}
	
	function get_member_by_email($email){
		if($email){
			$sql="select * from member where email='$email'";
			$query = $this->db->query($sql);
			if ($query->row())
				return $query->row();
			else
				return false;
		}else{
			return false;
		}
	}
	
	
	
	
	
	function clear_session_data(){
	
		$this->session->unset_userdata('mem_id');
		$this->session->unset_userdata('mem_email');
		$this->session->unset_userdata('mem_password');
		$this->session->unset_userdata('mem_phone');
		$this->session->unset_userdata('mem_type');
		$this->session->unset_userdata('mem_full_name');
		$this->session->unset_userdata('mem_last_login_date');
		$this->session->unset_userdata('mem_last_login_ip');
		$this->session->unset_userdata('mem_created_on');
	
	}
	
	function checkIdentity($email){
		$sql = "select * from member where email='$email' and identity='FB'";
		$result = $this->db->query($sql)->result();
		if($result){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	
	function get_law($mem_id) {
		//$subcond = "where status=$status";
		$sql = "select * from member where id= $mem_id ";
		$query = $this->db->query($sql);
		$result = $query->row();
		return $result;
	}
	
	public function lawyer_image($member_id) {
		$sql = "select * from profile where id='$member_id' ";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}
	
	function getLawyer($mem_id) {
		//$subcond = "where status=$status";
		
		$sql = "select * from profile where mem_id= $mem_id ";
		$query = $this->db->query($sql);
		$result = $query->row();
		return $result;
	}
	function showTips($limit=3)
	{
		$sql = "select * from tips order by RAND() limit $limit ";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}
	
	function getTipsinfo($id=0){
		$sql = "select * from tips where id=$id";
		$row = $this->db->query($sql)->row();
		if($row){
			return $row;
		}else{
			return false;
		}
	}
	function delete($id=NULL){
		$this->db->where('id',$id);
		$this->db->delete('profile');
		 
		$this->db->where('listing_id',$id);
		$this->db->delete('term');
	}
	
	
	
}
/* End of file*/
